package com.example.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.example.exception.CustomException;
import com.example.mapper.AdminMapper;
import com.example.mapper.CommentMapper;
import com.example.pojo.Account;
import com.example.pojo.Admin;
import com.example.pojo.Comment;
import com.example.pojo.LoginResponse;
import com.example.service.AdminService;
import com.example.service.CommentService;
import com.example.util.JwtUtil;
import com.example.util.Md5Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestHeader;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class CommentServiceImpl implements CommentService {
    @Resource
    private CommentMapper commentMapper;

    /**
     * 添加用户
     *
     * @param comment
     */
    @Override
    public void add(Comment comment) {
        commentMapper.add(comment);
    }


    /**
     * 根据id查询用户
     *
     * @param id
     * @return
     */
    @Override
    public Comment selectById(Integer id) {
        Comment comment = commentMapper.selectById(id);
        return comment;
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        commentMapper.deleteById(id);
    }

    /**
     * 批量删除数据
     *
     * @param ids
     */
    @Override
    public void deleteBatch(List<Integer> ids) {
        for (Integer id:ids)
            commentMapper.deleteById(id);
    }


    /**
     * 查询所有评论
     *
     * @return
     */
    @Override
    public List<Comment> selectAll(String token,String keyword,Integer roomId) {
        Map<String, Object> claims = JwtUtil.parseToken(token);
        Integer loginUserId = (Integer) claims.get("id") ;
        String role = (String) claims.get("role");
       Integer ownerId = role.equals("owner") ? loginUserId : null;


        List<Comment> list = commentMapper.selectAll(ownerId,keyword,roomId);
        return list;
    }

    @Override
    public int countByRoomId(Integer roomId) {
        return commentMapper.countByRoomId(roomId);
    }

    /**
     * 分页查询
     *
     * @param pageNum  查询哪一页
     * @param pageSize 一页的大小
     * @return
     */
    @Override
    public PageInfo<Comment> selectPage(String token,Integer pageNum, Integer pageSize,String keyword,Integer roomId) {
        PageHelper.startPage(pageNum,pageSize);

        Map<String, Object> claims = JwtUtil.parseToken(token);
        Integer loginUserId = (Integer) claims.get("id") ;
        String role = (String) claims.get("role");
        Integer ownerId = role.equals("owner") ? loginUserId : null;

        List<Comment> list = commentMapper.selectAll(ownerId,keyword,roomId);
        return PageInfo.of(list);
    }
}
